home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Chapter 13 The Compute Shader / SobelFilter / Shaders / Blur.hlsl next >
Encoding:
Text File  |  2016-03-02  |  3.8 KB  |  135 lines

  1. //=============================================================================
  2. // Performs a separable Guassian blur with a blur radius up to 5 pixels.
  3. //=============================================================================
  4.  
  5. cbuffer cbSettings : register(b0)
  6. {
  7.     // We cannot have an array entry in a constant buffer that gets mapped onto
  8.     // root constants, so list each element.  
  9.     
  10.     int gBlurRadius;
  11.  
  12.     // Support up to 11 blur weights.
  13.     float w0;
  14.     float w1;
  15.     float w2;
  16.     float w3;
  17.     float w4;
  18.     float w5;
  19.     float w6;
  20.     float w7;
  21.     float w8;
  22.     float w9;
  23.     float w10;
  24. };
  25.  
  26. static const int gMaxBlurRadius = 5;
  27.  
  28.  
  29. Texture2D gInput            : register(t0);
  30. RWTexture2D<float4> gOutput : register(u0);
  31.  
  32. #define N 256
  33. #define CacheSize (N + 2*gMaxBlurRadius)
  34. groupshared float4 gCache[CacheSize];
  35.  
  36. [numthreads(N, 1, 1)]
  37. void HorzBlurCS(int3 groupThreadID : SV_GroupThreadID,
  38.                 int3 dispatchThreadID : SV_DispatchThreadID)
  39. {
  40.     // Put in an array for each indexing.
  41.     float weights[11] = { w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10 };
  42.  
  43.     //
  44.     // Fill local thread storage to reduce bandwidth.  To blur 
  45.     // N pixels, we will need to load N + 2*BlurRadius pixels
  46.     // due to the blur radius.
  47.     //
  48.     
  49.     // This thread group runs N threads.  To get the extra 2*BlurRadius pixels, 
  50.     // have 2*BlurRadius threads sample an extra pixel.
  51.     if(groupThreadID.x < gBlurRadius)
  52.     {
  53.         // Clamp out of bound samples that occur at image borders.
  54.         int x = max(dispatchThreadID.x - gBlurRadius, 0);
  55.         gCache[groupThreadID.x] = gInput[int2(x, dispatchThreadID.y)];
  56.     }
  57.     if(groupThreadID.x >= N-gBlurRadius)
  58.     {
  59.         // Clamp out of bound samples that occur at image borders.
  60.         int x = min(dispatchThreadID.x + gBlurRadius, gInput.Length.x-1);
  61.         gCache[groupThreadID.x+2*gBlurRadius] = gInput[int2(x, dispatchThreadID.y)];
  62.     }
  63.  
  64.     // Clamp out of bound samples that occur at image borders.
  65.     gCache[groupThreadID.x+gBlurRadius] = gInput[min(dispatchThreadID.xy, gInput.Length.xy-1)];
  66.  
  67.     // Wait for all threads to finish.
  68.     GroupMemoryBarrierWithGroupSync();
  69.     
  70.     //
  71.     // Now blur each pixel.
  72.     //
  73.  
  74.     float4 blurColor = float4(0, 0, 0, 0);
  75.     
  76.     for(int i = -gBlurRadius; i <= gBlurRadius; ++i)
  77.     {
  78.         int k = groupThreadID.x + gBlurRadius + i;
  79.         
  80.         blurColor += weights[i+gBlurRadius]*gCache[k];
  81.     }
  82.     
  83.     gOutput[dispatchThreadID.xy] = blurColor;
  84. }
  85.  
  86. [numthreads(1, N, 1)]
  87. void VertBlurCS(int3 groupThreadID : SV_GroupThreadID,
  88.                 int3 dispatchThreadID : SV_DispatchThreadID)
  89. {
  90.     // Put in an array for each indexing.
  91.     float weights[11] = { w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10 };
  92.  
  93.     //
  94.     // Fill local thread storage to reduce bandwidth.  To blur 
  95.     // N pixels, we will need to load N + 2*BlurRadius pixels
  96.     // due to the blur radius.
  97.     //
  98.     
  99.     // This thread group runs N threads.  To get the extra 2*BlurRadius pixels, 
  100.     // have 2*BlurRadius threads sample an extra pixel.
  101.     if(groupThreadID.y < gBlurRadius)
  102.     {
  103.         // Clamp out of bound samples that occur at image borders.
  104.         int y = max(dispatchThreadID.y - gBlurRadius, 0);
  105.         gCache[groupThreadID.y] = gInput[int2(dispatchThreadID.x, y)];
  106.     }
  107.     if(groupThreadID.y >= N-gBlurRadius)
  108.     {
  109.         // Clamp out of bound samples that occur at image borders.
  110.         int y = min(dispatchThreadID.y + gBlurRadius, gInput.Length.y-1);
  111.         gCache[groupThreadID.y+2*gBlurRadius] = gInput[int2(dispatchThreadID.x, y)];
  112.     }
  113.     
  114.     // Clamp out of bound samples that occur at image borders.
  115.     gCache[groupThreadID.y+gBlurRadius] = gInput[min(dispatchThreadID.xy, gInput.Length.xy-1)];
  116.  
  117.  
  118.     // Wait for all threads to finish.
  119.     GroupMemoryBarrierWithGroupSync();
  120.     
  121.     //
  122.     // Now blur each pixel.
  123.     //
  124.  
  125.     float4 blurColor = float4(0, 0, 0, 0);
  126.     
  127.     for(int i = -gBlurRadius; i <= gBlurRadius; ++i)
  128.     {
  129.         int k = groupThreadID.y + gBlurRadius + i;
  130.         
  131.         blurColor += weights[i+gBlurRadius]*gCache[k];
  132.     }
  133.     
  134.     gOutput[dispatchThreadID.xy] = blurColor;
  135. }